import Vue from 'vue';
import VueRouter from 'vue-router';
import Login from '../views/Login.vue';

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter);

const routes = [{
    path: '/',
    name: 'index',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
    window.location.href = '/index#/'
    return;
  }
  if (to.path === '/login') {
    return next();
  }

  const tokenString = window.sessionStorage.getItem('token');

  if (!tokenString) {
    // return next('/login');
  }

  next();
});

export default router
